#pragma once

#include <iostream>
#include <pthread.h>
#include <time.h>
#include <string>
#include <functional>

using  fun = std::function<void()>;
typedef void (*callback_t)();

static int num = 1;
class  Thread
{
public:
    static void* Routine(void* args)
    {
        Thread* td = static_cast<Thread*>(args);
        td->Entry();

        return nullptr;
    }
public:
    Thread(callback_t cb):_tid(0), _name(""), _start_timestamp(0), _isrunning(false), _cb(cb)
    {}

    void Run()
    {
        _name = "thread-" + std::to_string(num++);
        _start_timestamp = time(nullptr);
        _isrunning = true;
        pthread_create(&_tid, nullptr, Routine, this);
    }

    void Join()
    {
        pthread_join(_tid, nullptr);
        _isrunning = false;
    }

    std::string name()
    {
        return _name;
    }

    u_int64_t StartStamp()
    {
        return _start_timestamp;
    }

    bool IsRunning()
    {
        return _isrunning;
    }

    void Entry()
    {
        _cb();
    }
    
    ~Thread()
    {}
private:
    pthread_t _tid;
    std::string _name;
    u_int64_t _start_timestamp;
    bool _isrunning;

    callback_t _cb;
};